package com.baizhi.zjs.action;

import java.awt.Image;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.entity.FUSerEntity;
import com.baizhi.zjs.entity.FrontAddressEntity;
import com.baizhi.zjs.entity.OrderEntity;
import com.baizhi.zjs.service.FrontAddressServie;
import com.baizhi.zjs.service.FrontAddressServieInpl;
import com.baizhi.zjs.service.ItemService;
import com.baizhi.zjs.service.ItemServiceImpl;
import com.baizhi.zjs.service.OrderServict;
import com.baizhi.zjs.service.OrederServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
//收货地址Action
public class FrontAddressAction extends ActionSupport {
	private String addressIdl;
	private String selected;
	private FrontAddressEntity fra;
	private FrontAddressEntity fra1;
	//订单的成员变量
	private OrderEntity order11;
	//用户地址的list集合
	private List<FrontAddressEntity> addressList;
//=====================订单
private	List<OrderEntity> orderlist;
private	OrderEntity orderentity ;
	//添加地址
	public String  addAddress(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		FUSerEntity login1 =(FUSerEntity)session.getAttribute("login");
		FrontAddressServie inpl = new FrontAddressServieInpl();//将用户的信息从登陆存放信息的session查出来
		System.out.println("查看接受参数"+fra);
		//将用户id补进去
		fra.setUser_id(login1.getId());
		System.out.println("打印补全后的地址信息"+fra);
		//1，如果id是null说明是新地址，添加地址即可
		if(fra.getId().equals("")){
			//添加地址
			System.out.println("到这了添加地址");
			inpl.addAddress(fra);
			//return "addAddress";
		}else {//否则已经存储过数据
			//判断数据是不是相等
			//1.先将这条地址数据从数据库中查出来
			FrontAddressEntity entity = inpl.sgowOneAssress(fra.getId());
			System.out.println("从数据库查出来的"+entity);
			//2.判断entity 与fra内容是否相同
			if(!entity.equals(fra)){//如果不相同，就修改地址
				System.out.println("地址不相同");
				inpl.updateAddress(fra);
			}else{
				System.out.println("地址相同不修改");
			}
		}
		OrderServict impl = new OrederServiceImpl();
		System.out.println("跳转到生成订单action");
		order11 = impl.addOrder(fra);
		//添加订单项
		ItemService itemServiceImpl = new ItemServiceImpl();
		itemServiceImpl.addItem(order11);
		//跳转到添加的action表单
		return "goOrder";
	}


	//查询一个用户中的所有收货地址
	public String showAllAddress(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		FrontAddressServieInpl inpl = new FrontAddressServieInpl();
		FUSerEntity attribu = (FUSerEntity)session.getAttribute("login");
		if(attribu==null){
			return "";
		}
		List<FrontAddressEntity> addres= inpl.showAllAddress(attribu.getId());
		//将list集合放进session防止第二次返回添加地址时下拉列表没有值
		session.setAttribute("addressList", addres);
		return "showalladderss";
	}

	/*查询一条地址*/
	public String showOneAddress(){
		System.out.println(addressIdl);
		FrontAddressServieInpl inpl = new FrontAddressServieInpl();

		/*if(addressIdl ==null){
		 return "showOne";
	}*/
		fra1 = inpl.sgowOneAssress(addressIdl);
		System.out.println(fra1);
		return "showOne";
	}


//=========================================后台查询所有订单
	public String showAllOrder(){
		OrderServict impl = new OrederServiceImpl();
		 orderlist = impl.showAllOrder();
		return "showorder";
	}
//=================================查询订单
	public String showOneorder(){
		OrederServiceImpl impl = new OrederServiceImpl();
	orderentity = impl.showoneOrder(addressIdl);
	System.out.println("订单action===========");
	System.out.println(addressIdl);
	System.out.println(orderentity);
		return "showoneorder";
	}

	public FrontAddressEntity getFra() {
		return fra;
	}
	public void setFra(FrontAddressEntity fra) {
		this.fra = fra;
	}
	public List<FrontAddressEntity> getAddressList() {
		return addressList;
	}
	public void setAddressList(List<FrontAddressEntity> addressList) {
		this.addressList = addressList;
	}
	public String getAddressIdl() {
		return addressIdl;
	}
	public void setAddressIdl(String addressIdl) {
		this.addressIdl = addressIdl;
	}
	public String getSelected() {
		return selected;
	}
	public void setSelected(String selected) {
		this.selected = selected;
	}
	public FrontAddressEntity getFra1() {
		return fra1;
	}
	public void setFra1(FrontAddressEntity fra1) {
		this.fra1 = fra1;
	}


	public List<OrderEntity> getOrderlist() {
		return orderlist;
	}


	public void setOrderlist(List<OrderEntity> orderlist) {
		this.orderlist = orderlist;
	}


	public OrderEntity getOrderentity() {
		return orderentity;
	}


	public void setOrderentity(OrderEntity orderentity) {
		this.orderentity = orderentity;
	}


	public OrderEntity getOrder11() {
		return order11;
	}


	public void setOrder11(OrderEntity order11) {
		this.order11 = order11;
	}


	


}
